package org.jeecg.modules.job.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.job.rms.entity.RmsCompany;
import org.jeecg.modules.job.rms.entity.RmsCompanyPosition;
import org.jeecg.modules.job.rms.service.IRmsCompanyPositionService;
import org.jeecg.modules.job.rms.service.IRmsCompanyService;
import org.jeecg.modules.job.ums.entity.UmsApplyLog;
import org.jeecg.modules.job.ums.entity.UmsCollect;
import org.jeecg.modules.job.ums.service.IUmsApplyLogService;
import org.jeecg.modules.job.ums.service.IUmsCollectService;
import org.jeecg.modules.job.ums.service.IUmsMemberRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 公司管理相关接口
 */
@RestController
@RequestMapping("/api/rms")
@Api(tags="移动端公司管理模块")
@Slf4j
public class RmsController {

    @Autowired
    private IRmsCompanyService companyService;
    @Autowired
    private IRmsCompanyPositionService positionService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    private IUmsCollectService collectService;
    @Autowired
    private IUmsApplyLogService applyLogService;
    @Autowired
    private IUmsMemberRoleService roleService;

    /**
     * 查询公司列表
     */
    @RequestMapping(value = "/getCompanyList",method = RequestMethod.GET)
    @ApiOperation(value = "查询公司列表", notes = "查询公司列表")
    public Result<Object> getCompanyList(RmsCompany param,
                                         @RequestParam(name="page", defaultValue="1") Integer page,
                                         @RequestParam(name="limit", defaultValue="10") Integer limit) {
        try{
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
        }catch (Exception e){
//            e.printStackTrace();
        }
        IPage<RmsCompany> pageInfo= companyService.getCompanyPageList(new Page<>(page, limit),param);
        return Result.ok(pageInfo);
    }


    /**
     * 查询公司列表
     */
    @RequestMapping(value = "/getCompanyListForSelect",method = RequestMethod.GET)
    @ApiOperation(value = "查询公司列表", notes = "查询公司列表")
    public Result<Object> getCompanyListForSelect(RmsCompany param,
                                                  @RequestParam(name="page", defaultValue="1") Integer page,
                                                  @RequestParam(name="limit", defaultValue="10") Integer limit) {
        IPage<RmsCompany> pageInfo= companyService.getCompanyPageMapList(new Page<>(page, limit),param);
        return Result.ok(pageInfo);
    }


    /**
     * 添加/更新公司信息
     */
    @ResponseBody
    @RequestMapping(value = "/updateCompany",method = RequestMethod.POST)
    @ApiOperation(value = "添加/更新公司信息", notes = "添加/更新公司信息")
    public Result<Object> updateCompany(@RequestBody RmsCompany companyParam) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        companyParam.setMemberId(user.getId());
        companyService.updateCompany(companyParam);
        return Result.ok();
    }

    /**
     * 公司认证
     */
    @ResponseBody
    @RequestMapping(value = "/updateAuth",method = RequestMethod.POST)
    @ApiOperation(value = "公司认证", notes = "公司认证")
    public Result<Object> updateAuth(@RequestBody RmsCompany companyParam) {
        companyParam.setAddAuthTime(new Date());
        companyService.updateById(companyParam);
        return Result.ok();
    }


    /**
     * 查询公司信息
     */
    @ResponseBody
    @RequestMapping(value = "/getCompanyDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询公司信息", notes = "查询公司信息")
    public Result<Object> getCompanyDetail(String id) {
        RmsCompany companyParam=new RmsCompany();
        if(StringUtils.isNotEmpty(id)){
            companyParam.setId(id);
        }else{
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            companyParam.setId(roleService.getCompanyId(user.getId()));
        }
        RmsCompany result=companyService.getOne(new QueryWrapper<>(companyParam));
        return Result.ok(result);
    }

    /**
     * 添加/更新职位信息
     */
    @ResponseBody
    @RequestMapping(value = "/updatePosition",method = RequestMethod.POST)
    @ApiOperation(value = "更新期望职位", notes = "更新期望职位")
    public Result<Object> updatePosition(@RequestBody RmsCompanyPosition positionParam) {
        if (StringUtils.isEmpty(positionParam.getId())){
            //取得当前Token登录者会员的ID
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            positionParam.setCompanyId(roleService.getCompanyId(user.getId()));
            positionParam.setMemberId(user.getId());
        }
        positionService.saveOrUpdatePosition(positionParam);
        return Result.ok();
    }

    /**
     * 查询职位信息
     */
    @ResponseBody
    @RequestMapping(value = "/getPositionDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询职位信息", notes = "查询职位信息")
    public Result<Object> getPostDetail(String id) {
        if(StringUtils.isNotEmpty(id)){
            RmsCompanyPosition result=positionService.getPositionDetail(new RmsCompanyPosition().setId(id));
            try{
                //取得当前Token登录者会员的ID
                LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                //是否收藏
                if (collectService.getOne(new QueryWrapper<>(new UmsCollect().setMemberId(user.getId()).setPositionId(id)))!=null){
                    result.setIfCollect(true);
                }
                //是否投递
                if (applyLogService.getOne(new QueryWrapper<>(new UmsApplyLog().setMemberId(user.getId()).setPositionId(id)))!=null){
                    result.setIfApply(true);
                }
            }catch (Exception e){
                System.err.println(e.getMessage());
            }
            return Result.ok(result);
        }else{
            return Result.error("参数异常");
        }
    }

    /**
     * 查询本公司职位列表
     */
    @ResponseBody
    @RequestMapping(value = "/getCompanyPositionList",method = RequestMethod.GET)
    @ApiOperation(value = "查询本公司职位列表", notes = "查询本公司职位列表")
    public Result<Object> getCompanyPositionList(RmsCompanyPosition param,
                                                 @RequestParam(name="page", defaultValue="1") Integer page,
                                                 @RequestParam(name="limit", defaultValue="10") Integer limit) {
        if(param.getCompanyId()==null){
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setCompanyId(roleService.getCompanyId(user.getId()));
        }
        IPage<RmsCompanyPosition> pageInfo= positionService.getPositionPageListForCompany(new Page<>(page, limit),param);
        return Result.ok(pageInfo);
    }



    /**
     * 查询职位列表
     */
    @ResponseBody
    @RequestMapping(value = "/getPositionList",method = RequestMethod.GET)
    @ApiOperation(value = "查询职位列表", notes = "查询职位列表")
    public Result<Object> getPositionList(RmsCompanyPosition param,
                                          @RequestParam(name="page", defaultValue="1") Integer page,
                                          @RequestParam(name="limit", defaultValue="10") Integer limit) {
        try{
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
        }catch (Exception e){
            System.err.println("未登录用户查询职位");
        }
        IPage<RmsCompanyPosition> pageInfo= positionService.getPositionPageList(new Page<>(page, limit),param);
        return Result.ok(pageInfo);
    }


    /**
     * 根据ID删除公司职位
     */
    @ResponseBody
    @RequestMapping(value = "/deletePositionById",method = RequestMethod.GET)
    @ApiOperation(value = "根据ID删除公司职位", notes = "根据ID删除公司职位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query")
    })
    public Result<Object> deletePositionById(Long id) {
        return Result.ok(positionService.removeById(id));
    }



}



